<?php

require_once('./EWT_Libs.php');


/* ---------- 页面路由 ---------- */
$_Body_FN = (
        (! isset($_GET['Nav'])) || ($_GET['Nav'] == '')
    ) ? 'index' : $_GET['Nav'];
$_Body_FN = "./HTML/{$_Body_FN}.html";
if (! file_exists($_Body_FN)) {
    echo file_get_contents('./HTML/404.html');
    exit(1);
}
try {
    $_Frame = phpQuery::newDocumentFile('./HTML/Frame.html');
} catch (Exception $_Error) {
    echo "<h1>本站正在维护……</h1>\n \
          <h3>EasyWebTemplate v0.2  (C)2014  shiyao@madeinwow.com</h3>";
    exit(1);
}
$_Body = phpQuery::newDocumentFile($_Body_FN);


/* ---------- 页面重构 ---------- */
/* ----- 公共头部 ----- */
Path_Trans($_Body, 'head title:first', array(), function ($_This) use ($_Frame) {
    if (! $_This->text())  return false;
    $_Frame_Title = $_Frame->find('head title:first');
    $_Frame_Title->text( $_This->text().' - '.$_Frame_Title->text() );
});
Path_Trans($_Body, 'meta[name="keyword"]', array(), function ($_This) use ($_Frame) {
    $_Meta = $_Frame->find('meta[name="keyword"]');
    if (! $_Meta->length)
        $_Frame->find('head title:first')->after( $_This );
    else $_Meta->attr('content', $_This->attr('content').','.$_Meta->attr('content'));
});
$_Frame->find('meta[name="description"]:first')->replaceWith(
    $_Body->find('meta[name="description"]:first')
);
$_Frame->find('head link[rel=stylesheet]:last')->after(
    $_Body->find('link[rel=stylesheet]')->remove()
);
Path_Trans($_Frame, 'link[rel=stylesheet]', array('href', 'CSS'));
$_Frame->find('head link[rel=stylesheet]:last')->after(
    $_Body->find('style')->remove()
);
Path_Trans($_Frame, 'script[src]', array('src', 'JavaScript'));

$_Body_JS = EasyImport($_Body);
if ($_Body_JS) {
    $_Frame_JS = EasyImport($_Frame);

    $_Frame_JS[0] = preg_replace(
        array(
            '/,\[/',
            '/(\[|,)\{/',
            '/(\[|\{)"/',
            '/",/',
            '/"([\]\}])/'
        ),
        array(
            ",\n        [",
            "$1\n            {",
            "$1\n        \"",
            "\",\n            ",
            "\"\n        $1"
        ),
        json_encode(
            array_merge($_Frame_JS[0], $_Body_JS[0])
        )
    );
    $_Frame_JS[1] = join("\n\n", array(
        $_Frame_JS[1], $_Body_JS[1]
    ));

    EasyImport($_Frame, $_Frame_JS);
}

/* ----- 主体内容 ----- */
$_Frame->find('body > .Body:first')->append( $_Body->find('body > *') );

Path_Trans($_Frame, 'a[href]', array(), function ($_This) {
    if ( $_This->is('[href^="http://"], [href^="https://"], [href^="#"]') )
        return;

    $_This->attr('href', preg_replace(
        '/(.+[^\/])\.(htm|html)(#[^#]+)?$/i',
        './index.php?Nav=$1$3',
        $_This->attr('href')
    ));
});
Path_Trans($_Frame, 'img', array('src', 'Image'));
Path_Trans($_Frame, '*[style*="background"]', array(), function ($_This) {
    $_Style = preg_replace(
        "/( 
            background[^;]+url  # 背景图 属性定位 \
            \(\s*('|\")?        # 左端 \
          )( 
            [^\"]+              # 背景图 文件名 \
            ('|\")?\s*\)        # 右端 \
        )/ix",
        '\\1./Image/\\3',
        $_This->attr('style')
    );
    $_This->attr('style', $_Style);
});


/* ---------- 页面响应 ---------- */
echo $_Frame->html();
